import httpClient, { ApiResponse } from '../app/httpClient';
import { Notification, NotificationReadStatus } from '../mock/notificationData';

// 通知API服务类
class NotificationService {
  // 获取通知列表
  async getNotifications(params?: {
    page?: number;
    pageSize?: number;
    search?: string;
    status?: string;
    level?: string;
  }): Promise<ApiResponse<{
    items: Notification[];
    total: number;
  }>> {
    try {
      // 发送实际API请求，替换之前的mock调用
      const response = await httpClient.get('/notifications', {
        params,
      });
      return response;
    } catch (error) {
      console.error('获取通知列表失败:', error);
      throw error;
    }
  }

  // 根据ID获取通知详情
  async getNotificationById(id: string): Promise<ApiResponse<Notification>> {
    try {
      const response = await httpClient.get(`/notifications/${id}`);
      return response;
    } catch (error) {
      console.error('获取通知详情失败:', error);
      throw error;
    }
  }

  // 创建新通知
  async createNotification(notification: Omit<Notification, 'id' | 'publishTime'>): Promise<ApiResponse<Notification>> {
    try {
      const response = await httpClient.post('/notifications', notification);
      return response;
    } catch (error) {
      console.error('创建通知失败:', error);
      throw error;
    }
  }

  // 更新通知
  async updateNotification(id: string, updates: Partial<Notification>): Promise<ApiResponse<Notification>> {
    try {
      const response = await httpClient.put(`/notifications/${id}`, updates);
      return response;
    } catch (error) {
      console.error('更新通知失败:', error);
      throw error;
    }
  }

  // 删除通知
  async deleteNotification(id: string): Promise<ApiResponse<boolean>> {
    try {
      const response = await httpClient.delete(`/notifications/${id}`);
      return response;
    } catch (error) {
      console.error('删除通知失败:', error);
      throw error;
    }
  }

  // 获取阅读状态
  async getReadStatuses(params?: {
    notificationId?: string;
    userId?: string;
  }): Promise<ApiResponse<NotificationReadStatus[]>> {
    try {
      const response = await httpClient.get('/notifications/read-statuses', {
        params,
      });
      return response;
    } catch (error) {
      console.error('获取阅读状态失败:', error);
      throw error;
    }
  }

  // 标记为已读
  async markAsRead(notificationId: string, userId: string): Promise<ApiResponse<NotificationReadStatus>> {
    try {
      const response = await httpClient.post('/notifications/mark-read', {
        notificationId,
        userId,
      });
      return response;
    } catch (error) {
      console.error('标记已读失败:', error);
      throw error;
    }
  }

  // 获取未读通知数量
  async getUnreadCount(userId: string): Promise<ApiResponse<number>> {
    try {
      const response = await httpClient.get(`/notifications/unread-count/${userId}`);
      return response;
    } catch (error) {
      console.error('获取未读数量失败:', error);
      throw error;
    }
  }
}

// 导出单例实例
export default new NotificationService();